//
//  MYPublishImageTagVC.m
//  MuYing
//
//  Created by tbb on 2019/8/14.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "MYPublishImageTagVC.h"
#import "MYPublishAddTagVC.h"
#import "MYPublishEditVC.h"
#import "MYPublishImageTagImageCell.h"
#import "TBBTagImageView.h"
#import "MYPublishAddTagModel.h"
@interface MYPublishImageTagVC ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) MYPublishImageTagTopView * topView;
@property (nonatomic, strong) MYPublishImageTagBottonView * bottonView;

@property (nonatomic, strong) UICollectionView * browseCollectView;
//当index
@property (nonatomic, assign) NSInteger currentIndex;

@property (nonatomic, strong) NSMutableArray * tagImageViewArr;
@end

@implementation MYPublishImageTagVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.topView.tittleLb.text = [NSString stringWithFormat:@"1/%zd",self.publishImageArr.count];
    NSLog(@"");
}

-(void)setupView {
    [super setupView];
    [self.view addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(kHeight_Top_SafeArea);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(44);
    }];

    [self.view addSubview:self.bottonView];
    [self.bottonView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-kHeight_Botton_SafeArea);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(49);
    }];
    
    self.bottonView.layer.shadowColor = HexColorA(0x000000, 0.6).CGColor;
    //2.设置阴影偏移范围
    self.bottonView.layer.shadowOffset = CGSizeMake(0, 2);
    //3.设置阴影颜色的透明度
    self.bottonView.layer.shadowOpacity = 0.2;
    
    //4.设置阴影半径
    self.bottonView.layer.shadowRadius = 4;
    //5.设置阴影路径
    self.bottonView.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bottonView.bounds].CGPath;
    
    [self.view addSubview:self.browseCollectView];
}
-(void)setPublishImageArr:(NSArray *)publishImageArr {
    _publishImageArr = [publishImageArr copy];
    [self.browseCollectView reloadData];
}

#pragma mark --<UICollectionViewDelegate, UICollectionViewDataSource>
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.publishImageArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MYPublishImageTagImageCell *imageCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MYPublishImageTagImageCell" forIndexPath:indexPath];

    //imageCell.imageView.image = self.publishImageArr[indexPath.item];
    imageCell.image = self.publishImageArr[indexPath.item];
    return imageCell;
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat cx = scrollView.contentOffset.x;
    NSInteger index = cx / kYQPScreenWidth;
    self.currentIndex = index;
    self.topView.tittleLb.text = [NSString stringWithFormat:@"%zd/%zd",(index+1),self.publishImageArr.count];
}
#pragma mark --<UICollectionViewDelegate, UICollectionViewDataSource> end

#pragma mark --lazy
-(MYPublishImageTagTopView *)topView {
    if (!_topView) {
        _topView = [MYPublishImageTagTopView viewFromXib];
        _topView.backgroundColor = UIColor.whiteColor;
        ws(self);
        [_topView.backBtn addTapAction:^(id  _Nonnull sender) {
            [weakself.navigationController popViewControllerAnimated:true];
        }];
        [_topView.nextBtn addTapAction:^(id  _Nonnull sender) {
            
            for (TBBTagImageView *imageView in self.tagImageViewArr) {
                for (TBBTagView *tagView in imageView.tagViewArr) {
                    TBBTagModel *tagMode = tagView.tagModel;
                    
                }
            }
            
            MYPublishEditVC *editVC = [[MYPublishEditVC alloc] init];
            editVC.publishImageArr = self.publishImageArr.mutableCopy;
            [weakself.navigationController pushViewController:editVC animated:true];
        }];
    }
    return _topView;
}
-(MYPublishImageTagBottonView *)bottonView {
    if (!_bottonView) {
        _bottonView = [MYPublishImageTagBottonView viewFromXib];
        _bottonView.backgroundColor = UIColor.whiteColor;
        ws(self);
        [_bottonView.tagBtn addTapAction:^(id  _Nonnull sender) {
            MYPublishAddTagVC *addTagVC = [[MYPublishAddTagVC alloc] init];
            addTagVC.tagSelectBlock = ^(MYPublishAddTagModel * _Nonnull tagModel) {
                MYPublishImageTagImageCell *imageCell = (MYPublishImageTagImageCell *)[self.browseCollectView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:self.currentIndex inSection:0]];
                //[imageCell.imageView addTagWithTitle:tagModel.title object:nil];
                [weakself.tagImageViewArr addObject:imageCell.imageView];
                [imageCell.imageView addTagWithTitle:tagModel.title point:CGPointMake(imageCell.imageView.width/2.0, imageCell.imageView.height/2.0) state:(ZYTagViewStateArrowLeft) editEnabled:YES object:nil];
            };
            [weakself.navigationController pushViewController:addTagVC animated:true];
        }];
    }
    return _bottonView;
}

-(UICollectionView *)browseCollectView {
    if (!_browseCollectView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.minimumLineSpacing = 0.0001;
        flowLayout.minimumInteritemSpacing = 0.0001;
        flowLayout.itemSize = CGSizeMake(kYQPScreenWidth, kYQPScreenHeight-49-(44+kHeight_Top_SafeArea)-kHeight_Botton_SafeArea);
        _browseCollectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, kHeight_Top_SafeArea+44, kYQPScreenWidth, kYQPScreenHeight-44-49-kHeight_Top_SafeArea-kHeight_Botton_SafeArea) collectionViewLayout:flowLayout];
        _browseCollectView.delegate = self;
        _browseCollectView.dataSource = self;
        _browseCollectView.pagingEnabled = YES;
        _browseCollectView.scrollEnabled = YES;
        _browseCollectView.backgroundColor = UIColor.whiteColor;
        _browseCollectView.showsHorizontalScrollIndicator = NO;
        [_browseCollectView registerClass:[MYPublishImageTagImageCell class] forCellWithReuseIdentifier:@"MYPublishImageTagImageCell"];
    }
    return _browseCollectView;
}

-(NSMutableArray *)tagImageViewArr {
    if (!_tagImageViewArr) {
        _tagImageViewArr = [NSMutableArray array];
    }
    return _tagImageViewArr;
}

#pragma mark --lazy end
//- (BOOL)prefersStatusBarHidden {
//    return YES;
//}

- (void)viewWillAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    id traget = self.navigationController.interactivePopGestureRecognizer.delegate;
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:traget action:nil];
    [self.view addGestureRecognizer:pan];
//    UIView  *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
//    statusBar.alpha = 0.0f; //隐藏
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
//    UIView  *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
//    statusBar.alpha = 1.0f; // 显示
}
@end

